/*
 * Class:PMonthView
 * 
 * Description: This class represent the month view for the calendar.
 *
 * Version: 2.1 - M-A Lavoie - 17-12-2011
 *      Show events in calendar
 * 
 * Version: 2.0 - Mathieu Canuel - 26-11-2011
 * 		Initial implementation
 * 

 */
package calendarInterface2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import calendarEventManager.CEMAgenda;
import calendarEventManager.CEMEvent;
import calendarEventManager.CEMListColor;
import controller.CTLEngine;

@SuppressWarnings("serial")
public class PMonthView extends JPanel{
	//GUI components
	JPanel pHeader;
	JPanel pDate;
	JPanel pNavigation;
	JPanel pMonthView;
	JLabel lDate;
	JButton bPrevious;
	JButton bNext;
	CalendarButton[] button;
	JLabel[] label;
	int month = Calendar.getInstance().get(Calendar.MONTH);
	int year = Calendar.getInstance().get(Calendar.YEAR);
	static String[] sDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
	
	public PMonthView(Calendar calendar) {
		super();
		this.year = calendar.get(Calendar.YEAR);
		this.month = calendar.get(Calendar.MONTH);
		build();
	}
	public void build(){
		setLayout(new BorderLayout());
		setSize(new Dimension(624,568));
		setVisible(true);
		setOpaque(true);
		
		pHeader = new JPanel(new BorderLayout());
		pNavigation = new JPanel();
		pDate = new JPanel();
		pHeader.add(pDate, BorderLayout.CENTER);
		pHeader.add(pNavigation, BorderLayout.EAST);
		pHeader.setOpaque(true);
		pHeader.setBackground(Color.CYAN);
		
		pMonthView = new JPanel(new GridLayout(7, 7));
		pMonthView.setPreferredSize(new Dimension(775,568));
		pMonthView.setOpaque(true);
	
		addButtons();
		addLabels();
		displayDate();
		add(pHeader, BorderLayout.NORTH);
		add(pMonthView, BorderLayout.SOUTH);
		
	}

	//**************************************************************
	// Method: displayDate
	// Description: Display the calendar informations on the panel
	//**************************************************************
	public void displayDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
		Calendar cal = Calendar.getInstance();
		
		for (int x = 7; x < button.length; x++){
			button[x].setText("");
			button[x].setBackground(Color.white);
			button[x].setForeground(Color.black);
		}
		cal.set(year, month, 1);
		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++){
			button[x].setText("" + day);
			if(day<9) {
				button[x].setDate(Timestamp.valueOf(sdf2.format(cal.getTime()) + "-" + 0 + day + " " + "00:00:00"));
			}
			else {
				button[x].setDate(Timestamp.valueOf(sdf2.format(cal.getTime()) + "-" + day + " " + "00:00:00"));
			}
			
			button[x].addActionListener(new ActionAddEvent(button[x].getDate()));

			CEMListColor liste = new CEMListColor();
			for(CEMAgenda oAgenda : CTLEngine.getAllAgendas()) {

				Color color = CalendarInterface.getColor(liste.get(oAgenda.getAgendaColour()));
				
				if(oAgenda.isActivated()) {
					for (CEMEvent oEvent : CTLEngine.getAllEvents(oAgenda.getAgendaId())) {
						if(compareDate(oEvent.getStartingTime(),button[x].getDate()) ) {
							for(ActionListener al : button[x].getActionListeners() ) {
								button[x].removeActionListener( al );
							}
							if(button[x].getBackground().equals(Color.white) || button[x].getBackground().equals(color)) {
								button[x].setBackground(color);
							}
							else {
								button[x].setBackground(Color.gray);
							}
							
							button[x].addActionListener(new ActionModifyEvent(oEvent));
							button[x].setForeground(Color.white);
						}
					}
				}
			}		
		}
		Font font = new Font("Arial",Font.BOLD,16);
		lDate.setFont(font);
		lDate.setText(sdf.format(cal.getTime()));
	}
	
	@SuppressWarnings("deprecation")
	private boolean compareDate(Timestamp date, Timestamp startingTime) {
		Timestamp endingTime = new Timestamp (startingTime.getTime());
		endingTime.setDate(endingTime.getDate()+1);
		return date.equals(startingTime) || (date.after(startingTime) && date.before(endingTime));
	}
	
	public void addButtons(){
		button = new CalendarButton[49];
		for (int x = 0; x < button.length; x++) {
			button[x] = new CalendarButton(null);
			button[x].setFocusPainted(false);
			button[x].setBackground(Color.white);
			if (x < 7) {
				button[x].setText(sDays[x]);
				button[x].setForeground(Color.red);
			}
			pMonthView.add(button[x]);
		}
		bPrevious = new JButton("<");
		bPrevious.addActionListener(new ActionPreviousMonth());
		bNext = new JButton(">");
		bNext.addActionListener(new ActionNextMonth());
		pNavigation.add(bPrevious);
		pNavigation.add(bNext);
	}
	
	public void addLabels(){
		lDate = new JLabel("", JLabel.CENTER);
		lDate.setOpaque(true);
		pDate.add(lDate);
	}
	
	//Actions des boutons
	public class ActionPreviousMonth implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			month--;
			displayDate();
			setSideCalendar();
		}
	}
	
	public class ActionNextMonth implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			month++;
			displayDate();
			setSideCalendar();
		}
	}
	
	public class ActionModifyEvent implements ActionListener{
		CEMEvent oEvent;
		public ActionModifyEvent(CEMEvent oEvent) {
			this.oEvent = oEvent;
		}
		public void actionPerformed(ActionEvent e) {
			Calendar cal = CalendarInterface.getSideCalDate();
			cal.setTime(oEvent.getStartingTime());
			CalendarInterface.setSideCalDate(cal);
			CalendarInterface.showDayView(CalendarInterface.getSideCalDate());
		}
	}
	
	private void setSideCalendar(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH,1);
		CalendarInterface.setSideCalDate(cal);
	}
	
	public class ActionAddEvent implements ActionListener{
		Timestamp date;
		public ActionAddEvent(Timestamp date) {
			this.date = date;
		}
		
		public void actionPerformed(ActionEvent e) {
			CalendarInterface.showAddEventView(date);
		}
	}	
}